package com.lucweb.action;



import com.lucweb.entity.BaseDict;
import com.lucweb.entity.TaskJc;
import com.lucweb.entity.TaskQuestion;
import com.lucweb.service.BaseDictService;
import com.lucweb.service.JcService;
import com.lucweb.service.QuestionService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;

@Controller("jcAction")
@Scope("prototype")
public class JcAction extends ActionSupport {

    @Resource
    private JcService JcService;

    @Resource
    private QuestionService questionService;

    @Resource
    private BaseDictService baseDictService;

    private String company;
    private String type;


    private BaseDict baseDict;

    private TaskJc taskJc;

    private int taskId;

    private File file;
    //文件名称
    private String fileFileName;

    //文件类型
    private String fileContentType;
    //注意：文件名称和文件类型的名称前缀必须相同，

    private InputStream fileInput;



    private String fileName;
    private String inputName;
    private String contentDispostion;

    //新建检测任务方法
    public String newJcTask() {

        String number =  JcService.newJcTask();

        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("number", number);
        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);

        return "number";

    }


    //修改问题
    public String printJcTask(){


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+taskJc.getCompanyName()+"' " +
                "and isfinish != '已处理' and mustDo='1' and type = '"+type+"'");

        ActionContext.getContext().put("taskJc", taskJc);
        ActionContext.getContext().put("questionList", questionList);


        return "printJc";
    }


    //修改问题
    public String editJcTask(){


        taskJc = JcService.getJctaskById(taskId);

        ActionContext.getContext().put("taskJc", taskJc);

        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);


        System.out.println(taskJc);

        return "number";
    }

    //删除任务
    public String deleteJcTask(){

        boolean flag = JcService.deleteJctask(taskId);


        return "number";
    }

    //删除任务
    public String showjcTaskList(){

        List<TaskJc> taskJcList = JcService.showTaskByName(company);
        ActionContext.getContext().put("taskJcList",taskJcList);


        return "menbers";
    }

    //新建检测任务方法
    public String saveOrUpdate() throws IOException {

        //获取需要上传文件的文件路径
        File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
        //判断文件是否上传，如果上传的话将会创建该目录
        if(!uploadFile.exists()){
            uploadFile.mkdir(); //创建该目录
        }


        try {
            if (file!=null){
                FileUtils.copyFile(file,new File(uploadFile,fileFileName));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        if(taskJc.getUpdown().length()==0){
            if(fileFileName!=null){
                taskJc.setUpdown(fileFileName);
            }else {
                taskJc.setUpdown(null);
            }

        }else {
            if(fileFileName!=null){
                taskJc.setUpdown(fileFileName);
            }

        }

        System.out.println(taskJc);
        JcService.saveOrUpdate(taskJc);

        return "success";

    }




    public void setCompany(String company) {
        this.company = company;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BaseDict getBaseDict() {
        return baseDict;
    }

    public void setBaseDict(BaseDict baseDict) {
        this.baseDict = baseDict;
    }


    public TaskJc getTaskJc() {
        return taskJc;
    }

    public void setTaskJc(TaskJc taskJc) {
        this.taskJc = taskJc;
    }


    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public InputStream getFileInput() {
        return fileInput;
    }

    public void setFileInput(InputStream fileInput) {
        this.fileInput = fileInput;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setInputName(String inputName) {
        this.inputName = inputName;
    }

    public String getInputName() {
        return inputName;
    }

    public void setContentDispostion(String contentDispostion) {
        this.contentDispostion = contentDispostion;
    }

    public String getContentDispostion() {
        return contentDispostion;
    }

    public QuestionService getQuestionService() {
        return questionService;
    }

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }
}
